package us.mmapp;

import java.util.ArrayList;

import com.google.android.maps.GeoPoint;

import us.mmapp.SearchManager.PredicateType;
import us.mmapp.entities.ObjectEntity;
import us.mmapp.util.ApplicationData;
import us.mmapp.util.ObjectEntityAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class SearchActivity extends Activity{

	private Spinner searchType;
	private TextView searchKeyword;
	private ListView searchResult;
	private TextView searchEmpty;
	private int mSelection;
	private String mKeyword;
	private ObjectEntityAdapter mListViewAdapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
				
		mSelection = 0;
		mKeyword = "";
		
		setContentView(R.layout.search_view);
		
		searchType = (Spinner) findViewById(R.id.search_type);
		searchKeyword = (TextView) findViewById(R.id.search_keyword);
		searchEmpty = (TextView) findViewById(R.id.search_no_result_found);
		
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.filter_types,
                android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		searchType.setAdapter(adapter);
		
		searchResult = (ListView) findViewById(R.id.search_listBox);
		mListViewAdapter = new ObjectEntityAdapter(this, android.R.layout.simple_spinner_item, new ArrayList<ObjectEntity>());
		searchResult.setAdapter(mListViewAdapter);
		searchResult.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				ObjectEntity o = (ObjectEntity) searchResult.getItemAtPosition(position);
				searchResult_Clicked(o);
			}
		});
		
		Button searchButton = (Button) findViewById(R.id.search_ok);
		
		searchButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				mSelection = searchType.getSelectedItemPosition();
				//Get password
				mKeyword = searchKeyword.getText().toString();
				InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(searchKeyword.getWindowToken(), 0);
				Search();
			}
		});
		
		Button cancelButton = (Button) findViewById(R.id.search_cancel);
		
		cancelButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Cancel();
			}
		});
	}

	private void Search()
	{
		ArrayList<ObjectEntity> list = ApplicationData.getObjects();
		if (list != null)
		{
    		PredicateType type;
    		switch (mSelection)
    		{
    			case 0: type = PredicateType.Title; break;
    			case 1: type = PredicateType.Description; break;
    			case 2: type = PredicateType.Tags; break;
    			case 3: type = PredicateType.Comment; break;
    			case 4: type = PredicateType.Friend; break;
    			default: type = PredicateType.Any; break;
    		}
			SearchManager manager = new SearchManager(list);
			ArrayList<ObjectEntity> result = manager.search(type, mKeyword, true, true);
			mListViewAdapter.clear();
			for (int i = 0 ; i < result.size(); i++)
			{
				mListViewAdapter.add(result.get(i));
			}
			if (result.size() == 0)
			{
				//show no result found textview
				searchEmpty.setVisibility(0);
			}
			else
			{
				//hide no result found textview
				searchEmpty.setVisibility(4);
			}
			
		}
	}
	
	private void Cancel()
	{
		setResult(RESULT_CANCELED);
		this.finish();
	}
	
	private void searchResult_Clicked(ObjectEntity entity)
	{
		int latitude = (int) (entity.getLatitude()*1000000);
		int longitude = (int) (entity.getLongitude()*1000000);
		Intent intent = new Intent();
		intent.putExtra("latitude", latitude);
		intent.putExtra("longitude", longitude);
		setResult(RESULT_OK, intent);
		this.finish();
	}

}


